package com.ucode.application.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ucode.application.form.sys.DeptForm;
import com.ucode.sys.model.Dept;
import com.ucode.sys.service.DeptService;
import com.ucode.sys.service.UserService;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="部门管理")
@RestController
@RequestMapping("/sys-dept")
@Validated
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    
    @PreAuthorize("hasAuthority('sys:dept:read') or hasRole('ADMIN')")
    @ApiOperation("根据条件分页查询列表")
    @GetMapping(value = "/list")
    public ResponseResult<Object> findPage(
            @ApiParam(value ="部门名称")  @RequestParam(value = "name") String name,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        return ResponseResult.success();
    }
    
    @PreAuthorize("hasAuthority('sys:dept:read') or hasRole('ADMIN')")
    @ApiOperation(value="根据主键查询")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getById/{id}")
    public ResponseResult<Object> getById(@PathVariable(name="id",required=true) Long id){
        Dept dept = deptService.findById(id);
        return ResponseResult.success(dept);
    }
    
    @PreAuthorize("hasAuthority('sys:dept:write') or hasRole('ADMIN')")
    @ApiOperation(value="创建")
    @PostMapping("/create")
    public ResponseResult<Object> create(@Validated @RequestBody DeptForm form) {
        
        Dept dept = new Dept(form.getParentId(),form.getName(), form.getRemark(), form.getSort());
        dept = deptService.insert(dept);
        
        return dept != null ? ResponseResult.success(dept) : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:dept:write') or hasRole('ADMIN')")
    @ApiOperation(value="更新")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/update/{id}")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
            @Validated @RequestBody DeptForm form) {
        
        Dept dept = new Dept(form.getParentId(), form.getName(), form.getRemark(), form.getSort());
        dept.setId(id);
        int i = deptService.update(dept);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:menu:write') or hasRole('ADMIN')")
    @ApiOperation(value="删除")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/delete/{id}")
    public ResponseResult<Object> delete(@PathVariable(name="id",required=true) Long id) {
        
        //查询是否有子部门
        if(deptService.findDirectChildCount(id) > 0){
            return ResponseResult.result(ResultCodeEnum.HAS_CHILD_DEL_ERROR);
        }
        //查询是否有关联用户
        if(userService.findDeptDirectUserCount(id) > 0){
            return ResponseResult.result(ResultCodeEnum.DEPT_USER_DEL_ERROR);
        }
        
        deptService.delete(id);
        return ResponseResult.success();
    }
    
}
